package com.lv.service.dialog

import android.app.Activity
import android.content.Context
import android.text.*
import android.text.method.DigitsKeyListener
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.KeyboardUtils
import com.lv.common.imp.OnPositionTListener
import com.lv.common.ktx.filterLine
import com.lv.common.utils.showToast
import com.lv.service.R
import com.lv.service.ui.INPUT_ENUM
import com.lxj.xpopup.impl.PartShadowPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class InputBottomPopupView(
    private val mContext: Context,
    private val mTitle: String = "",
    private val mHint: String = "",
    private val mLength: Int = -1,
    private val INPUT_STATE: INPUT_ENUM? = INPUT_ENUM.NOMAL,
    private val positionListener: OnPositionTListener<String>? = null
) :
    PartShadowPopupView(mContext) {
    private var tvTitle: TextView? = null
    private var tvSure: TextView? = null
    private var etContent: EditText? = null
    private var ivClear: ImageView? = null
    private var viewTop: View? = null
    private var mContent = ""
    override fun getImplLayoutId(): Int {
        return R.layout.input_bottom_popup
    }

    override fun onCreate() {
        super.onCreate()
        viewTop = findViewById<View>(R.id.view_top)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        tvSure = findViewById<TextView>(R.id.tv_sure)
        etContent = findViewById<EditText>(R.id.et_content)
        ivClear = findViewById<ImageView>(R.id.iv_clear)
        etContent?.hint = mHint
        initView()
        initEvnet()
    }

    private fun initView() {
        tvTitle?.text = mTitle
        if (mLength != -1) {
            etContent?.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(mLength))
        }
        when (INPUT_STATE) {
            INPUT_ENUM.SINGLE -> {
                etContent?.maxLines = 1
                etContent?.ellipsize = TextUtils.TruncateAt.END
                if (mContent.isNotEmpty()) {
                    ivClear?.visibility = View.VISIBLE
                } else {
                    ivClear?.visibility = View.GONE
                }
                etContent?.isSingleLine = true
                etContent?.imeOptions = EditorInfo.IME_ACTION_DONE

            }
            INPUT_ENUM.NUMBER -> {
                etContent?.inputType = InputType.TYPE_CLASS_NUMBER
            }
            INPUT_ENUM.PHONE -> {
                etContent?.inputType = InputType.TYPE_CLASS_NUMBER
                etContent?.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(11))
                if (mContent.isNotEmpty()) {
                    ivClear?.visibility = View.VISIBLE
                } else {
                    ivClear?.visibility = View.GONE
                }
            }

            INPUT_ENUM.NUMB_CHAT -> {//数字和字母
                etContent?.transformationMethod = null
                etContent?.keyListener = object : DigitsKeyListener() {
                    override fun getInputType(): Int {
                        return InputType.TYPE_TEXT_VARIATION_PASSWORD
                    }

                    override fun getAcceptedChars(): CharArray {
                        return "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray()
                    }
                }
            }


            else -> {

            }

        }

    }

    private var mKeyboardH = 0
    private fun initEvnet() {
        KeyboardUtils.registerSoftInputChangedListener(
            mContext as Activity
        ) { height ->
            handler.postDelayed(Runnable { mKeyboardH = height }, 500)

        }
        viewTop?.setOnClickListener {
            if (mKeyboardH == 0) {
                dismiss()
            }
        }
        findViewById<ImageView>(R.id.iv_close).setOnClickListener {
            dismiss()
        }
        etContent?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (INPUT_STATE == INPUT_ENUM.PHONE || INPUT_STATE == INPUT_ENUM.SINGLE) {
                    if (s != null && s.isNotEmpty()) {
                        ivClear?.visibility = View.VISIBLE
                    } else {
                        ivClear?.visibility = View.GONE
                    }
                }
//                if (s != null && s.isNotEmpty()) {
//                    tvSure?.alpha = 1.0f
//                    tvSure?.isEnabled = true
//                } else {
//                    tvSure?.alpha = 0.2f
//                    tvSure?.isEnabled = false
//                }

            }

            override fun afterTextChanged(s: Editable?) {
            }
        })
        ivClear?.setOnClickListener {
            etContent?.text?.clear()
            ivClear?.visibility = View.GONE
            mContent = ""
        }
        tvSure?.setOnClickListener {
            val content = etContent?.text?.trim().toString().filterLine()
            if (content.isNotEmpty()) {
                positionListener?.onClick(
                    0,
                    content,
                )
                dismiss()
            } else {
                showToast(mHint)
            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()

    }


}